package com.bestcem.xm.user.dao.impl;

import com.bestcem.xm.user.dao.UserPrivateKeyDao;
import com.bestcem.xm.user.dao.mapper.UserPrivateKeyMapper;
import com.bestcem.xm.user.entity.mysql.UserPrivateKey;
import com.bestcem.xm.user.entity.pojo.UserPrivateKeyDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserPrivateKeyConvert;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * 公钥私钥基础服务dao
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/24 14:48
 */
@Repository
public class UserPrivateKeyDaoImpl implements UserPrivateKeyDao {

    @Resource
    private UserPrivateKeyMapper userPrivateKeyMapper;

    @Resource
    private UserPrivateKeyConvert userPrivateKeyConvert;

    @Override
    public String insert(UserPrivateKeyDO userPrivateKeyDO) {
        UserPrivateKey userPrivateKey = userPrivateKeyConvert.do2Mysql(userPrivateKeyDO);
        userPrivateKey.setPrivateKeyId(DataBaseUtil.generateId());
        DataBaseUtil.setCreateAndUpdateTime(userPrivateKey);
        userPrivateKeyMapper.insert(userPrivateKey);
        return userPrivateKey.getPrivateKeyId();
    }

    @Override
    public String insertSelective(UserPrivateKeyDO userPrivateKeyDO) {
        UserPrivateKey userPrivateKey = userPrivateKeyConvert.do2Mysql(userPrivateKeyDO);
        userPrivateKey.setPrivateKeyId(DataBaseUtil.generateId());
        DataBaseUtil.setCreateAndUpdateTime(userPrivateKey);
        userPrivateKeyMapper.insertSelective(userPrivateKey);
        return userPrivateKey.getPrivateKeyId();
    }

    @Override
    public UserPrivateKeyDO selectById(String privateKeyId) {
        return userPrivateKeyConvert.mysql2Do(userPrivateKeyMapper.selectByPrimaryKey(privateKeyId));
    }

    @Override
    public Integer updateSelectiveById(UserPrivateKeyDO userPrivateKeyDO) {
        UserPrivateKey userPrivateKey = userPrivateKeyConvert.do2Mysql(userPrivateKeyDO);
        DataBaseUtil.setUpdateTime(userPrivateKey);
        return userPrivateKeyMapper.updateByPrimaryKeySelective(userPrivateKey);
    }

    @Override
    public Integer deleteById(String privateKeyId) {
        return userPrivateKeyMapper.deleteByPrimaryKey(privateKeyId);
    }

}
